package interface_client;

import com.desanonymerUtilisateurs.ConnectDatabase;
import com.lloseng.ocsf.simplegui.client.SimpleClient;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class EcouteurRepondre implements ActionListener {

    private SimpleClient simpleClient;
    ConnectDatabase connect = new ConnectDatabase();
    JRadioButton oui;
    JRadioButton non;
    String reponse_question;
    
    public EcouteurRepondre(SimpleClient client)
    {
        this.simpleClient = client;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        final JFrame reponse = new JFrame();
        
        JPanel panel_reponse = new JPanel();
        panel_reponse.setLayout(new GridLayout(3, 3));
        
        //ResultSet result = connect.doRequest("SELECT question FROM sondage WHERE identifiant = 1");
        
        JLabel label_question = new JLabel("question : "); //là on récupère la question depuis la base de données, requête ci-dessus
        
        JLabel enonce_question = new JLabel("Pensez vous que ce projet va nous apporter une bonne note ?");
        
        oui = new JRadioButton("Oui");
        non = new JRadioButton("Non");

        oui.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
               if (oui.isSelected())
                    non.setEnabled(false);
               else
                    non.setEnabled(true);
            }
        });

        non.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (non.isSelected())
                    oui.setEnabled(false);
                else
                    oui.setEnabled(true);
            }
        });    
        
        JButton valider = new JButton("Valider");

        JButton annuler = new JButton("Annuler"); 

        panel_reponse.add(label_question);
        panel_reponse.add(enonce_question);
        
        panel_reponse.add(oui);
        panel_reponse.add(non);

        panel_reponse.add(valider);
        panel_reponse.add(annuler);
        
        reponse.getContentPane().add(panel_reponse, BorderLayout.CENTER);
        
        reponse.setTitle("Répondre au sondage");
        reponse.setSize(600, 400);
        
        reponse.show();
        
         valider.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                
                if(oui.isSelected())
                    reponse_question = "oui";
                else if (non.isSelected())
                    reponse_question = "non";

                System.out.println("reponse : " + reponse_question);
                
                try
                {
                    simpleClient.openConnection();
                    simpleClient.sendToServer("#reponseSondage " + reponse);
                    //Manque la question format de sortie attendu:"#reponseSondage idQuestion reponse"
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }

                reponse.hide();
            }
         });
         
        annuler.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
               reponse.hide();
            }
        }); 
    }

}
